package com.weixin.paleituo.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.activity.CouponsActivity;
import com.weixin.paleituo.activity.LoginActivity;
import com.weixin.paleituo.adapter.SignCouponsRecycleAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseFragment;
import com.weixin.paleituo.bean.CouponsSignBean;
import com.weixin.paleituo.bean.SignBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.HeaderAndFooterWrapper;
import com.weixin.paleituo.view.ItemBuyHouseStepView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author yichao
 * Time  2017/12/19 14:40
 * Dest  签约房子合同
 */

public class SignedHouseFragment extends BaseFragment {
    @BindView(R.id.recycle_fragment_signedhouse)
    RecyclerView mRecycleFragmentSignedhouse;
    private View mSignConetView;
    private View mHeadView;
    private ItemBuyHouseStepView mHouseTotle;
    private ItemBuyHouseStepView mSignTime;
    private ItemBuyHouseStepView mWorkerNumber;
    private TextView mWorkerName;
    private TextView mLookAllCoupons;
    private TextView mTvContact;
    private String mPhone;
    private RelativeLayout mRlLookAllRoot;
    List<CouponsSignBean> mCouponsSignBeans=new ArrayList<>();

    @Override
    public View initView(Context context) {

        mSignConetView = UIUtils.inflate(context, R.layout.fragment_signedhouse);
        mHeadView = UIUtils.inflate(mContext, R.layout.layout_coupons_sign_head);
        mHouseTotle = mHeadView.findViewById(R.id.itembuyhousestepview_housetotle_fragment_signhouse);
        mSignTime = mHeadView.findViewById(R.id.itembuyhousestepview_signtime_fragment_signhouse);
        mWorkerNumber = mHeadView.findViewById(R.id.itembuyhousestepview_worknumber_fragment_signhouse);
        mWorkerName = mHeadView.findViewById(R.id.tv_workname_item_buyhousestep_layout);
        mLookAllCoupons = mHeadView.findViewById(R.id.tv_lookall_layout_coupons_sign_head);
        mRlLookAllRoot = mHeadView.findViewById(R.id.rl_lookall__root_layout_coupons_sign_head);
        mTvContact = mHeadView.findViewById(R.id.tv_contact_layout_coupons_sign_head);




        return mSignConetView;
    }

    public static BaseFragment newInstance(SignBean signBean) {
        SignedHouseFragment signedHouseFragment = new SignedHouseFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(Contans.INTENT_DATA, signBean);
        signedHouseFragment.setArguments(bundle);

        return signedHouseFragment;
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle arguments = getArguments();
        SignBean signBean = arguments.getParcelable(Contans.INTENT_DATA);
        if (signBean != null) {

            mHouseTotle.setTvContent(signBean.getTotalMoney()+"万元");
            mSignTime.setTvContent(signBean.getSignTime());
            mPhone = signBean.getPhone();
            mWorkerName.setText(signBean.getName()+"");
            mWorkerNumber.setTvContent(signBean.getJobNumber());
            List<CouponsSignBean> signBeanCoupons = signBean.getCoupons();
            if (signBeanCoupons!=null &&signBeanCoupons.size()>0){
                mRlLookAllRoot.setVisibility(View.VISIBLE);
                mCouponsSignBeans.addAll(signBeanCoupons);
            }else {
                mRlLookAllRoot.setVisibility(View.GONE);
            }

            SignCouponsRecycleAdapter signCouponsRecycleAdapter=new SignCouponsRecycleAdapter(mContext, mCouponsSignBeans);
            HeaderAndFooterWrapper headerAndFooterWrapper=new HeaderAndFooterWrapper(signCouponsRecycleAdapter);
            headerAndFooterWrapper.addHeaderView(mHeadView);
            //设置布局管理器
            mRecycleFragmentSignedhouse.setLayoutManager(new LinearLayoutManager(mContext));
            //设置adapter
            mRecycleFragmentSignedhouse.setAdapter(headerAndFooterWrapper);
            //设置Item增加、移除动画
            mRecycleFragmentSignedhouse.setItemAnimator(new DefaultItemAnimator());

        }

    }
    @Override
    protected void initEvent() {
        super.initEvent();
        mTvContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if (!StringUtils.isEmpty(mPhone)){
                  try {
                      Intent dialIntent =  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mPhone));
                      mContext.startActivity(dialIntent);
                  }catch (Exception e){

                  }
              }else {
                  ToastUtils.makeText("电话为空");
              }

            }
        });


        mLookAllCoupons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (BaseApplication.isLoginSuccess){

                CouponsActivity.startActivity(mContext);
                }else {
                    LoginActivity.startActivity(mContext,true);
                }
            }
        });
    }
}
